package com.learn.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * TTl队列
 */
@Configuration
public class TtlQueueConfig {

    public static final String X_EXCHANGES = "X";
    public static final String Y_EXCHANGES = "Y";
    public static final String QA_QUEUE = "QA";
    public static final String QB_QUEUE = "QB";
    public static final String QD_QUEUE = "QD";

    public static final String QC_QUEUE = "QC";

    //交换机x
    @Bean
    public DirectExchange xExchange() {
        return new DirectExchange(X_EXCHANGES);
    }

    //死信交换机y
    @Bean
    public DirectExchange yExchange() {
        return new DirectExchange(Y_EXCHANGES);
    }

    //声明队列
    @Bean
    public Queue queueA() {
        return QueueBuilder.durable(QA_QUEUE)
                .deadLetterExchange(Y_EXCHANGES)
                .deadLetterRoutingKey("YD")
                .ttl(10000)
                .build();
    }

    @Bean
    public Queue queueB() {
        return QueueBuilder.durable(QB_QUEUE)
                .deadLetterExchange(Y_EXCHANGES)
                .deadLetterRoutingKey("YD")
                .ttl(20000)
                .build();
    }

    @Bean
    public Queue queueD() {
        return QueueBuilder.durable(QD_QUEUE)
                .build();
    }

    @Bean
    public Queue queueC() {
        return QueueBuilder.durable(QC_QUEUE)
                .deadLetterExchange(Y_EXCHANGES)
                .deadLetterRoutingKey("YD")
                .build();
    }


    //绑定交换机和队列
    @Bean
    public Binding queueABindingX(@Qualifier("queueA") Queue queueA,
                                  @Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueA).to(xExchange).with("XA");
    }

    @Bean
    public Binding queueBBindingX(@Qualifier("queueB") Queue queueB,
                                  @Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueB).to(xExchange).with("XB");
    }

    @Bean
    public Binding queueDBindingY(@Qualifier("queueD") Queue queueD,
                                  @Qualifier("yExchange") DirectExchange yExchange){
        return BindingBuilder.bind(queueD).to(yExchange).with("YD");
    }
    @Bean
    public Binding queueCBindingX(@Qualifier("queueC") Queue queueC,
                                  @Qualifier("xExchange") DirectExchange xExchange){
        return BindingBuilder.bind(queueC).to(xExchange).with("XC");
    }
}
